/*
 * EmployeeDetailServlet.java
 * Created on 25. juni 2008, 12:10
 */
package webshop.del;

import java.io.IOException;
import java.io.PrintWriter;
import javax.persistence.EntityManager;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import webshop.data.del.Employee;



/**
 * EmployeeDetailServlet prints a page with employee details.
 * @author even
 */
public class EmployeeDetailServlet  extends AbstractDepartmentServlet {

    
    /**
     * Handle requests. Sends a html page containing the department list.
     *
     * @param req the request object
     * @param resp the response object
     *
     * @throws IOException
     * @throws ServletException
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        EntityManager em = emf.createEntityManager();
        PrintWriter out = response.getWriter();
        
        String tmp;
        Employee emp = null;
        tmp = request.getParameter("empno");
        if (tmp != null) {
            long empNo = Long.valueOf(tmp);
            emp = em.find(Employee.class, empNo);
        }
        
        response.setContentType("text/html");
        response.setHeader("Cache-control", "no-cache");
        response.setHeader("Expires", "0");

        out.println("<html><head><title>"+this.getClass().getName()+"</title></head><body>");
        out.println("<h1>Employee Detail</h1>");
        if (emp != null) {
            out.println("<table>");
            printDatum(out, "Emp.no", emp.getEmpNo());
            printDatum(out, "Name", emp.getName());
            printDatum(out, "Salary", emp.getSalary());
            printDatum(out, "Department", emp.getDepartment().getName());
            out.println("</table>");
            out.println("<p><a href=\"empedit?empno="+emp.getEmpNo()+"\">Edit Employee</a></p>");
            out.println("<p><a href=\"empdelete?empno="+emp.getEmpNo()+"\">Delete Employee</a></p>");
        }
        else out.println("<p>Emp.no not specified!</p>");
        
        out.println("<hr/>");
        out.println("<p><a href=\"index.html\">Home</a></p>");
        out.println("</body>");
        out.println("</html>");
        
        em.close();
    }
}
